<template>
    <rui-page :defines="defines">
        <rui-query :label-width="200"
                   ref="searchForm"
                   :query="queryData"
                   :query-rows="searchFormOptions"
        />
        <rui-table
            :defineId="'voucherBusinessDifine'"
            :fixedLeft="['action']"
            :showIndex="false"
            :select="false"
            :columns="businessColumns"
            :slots="[{key:'action',slot:'action'}]"
            @loadDatas="queryData"
            ref-query="searchForm"
            ref="voucherBusinessTable"
        >
            <template slot="toolBar">
                <Button @click="add" icon="md-add" type="primary">新增</Button>
            </template>
            <template slot="action" slot-scope="{ row }">
                <div>
                    <Button @click="edit(row)" size="small" type="primary">编辑</Button>
                    <Button @click="view(row)" size="small" type="primary">查看</Button>
                    <Button type="error" size="small" @click="remove(row)">删除</Button>
                    <circleLoading v-if="operationLoading"/>
                </div>
            </template>
        </rui-table>
        <Modal v-model="isShowModel" :title="modelTitle" :width="1000" :closable="false"
               :mask-closable="false" @on-ok="businessSubmit">
            <rui-form :form-options="businessFormOptions" :read-only="false"
                      ref="businessForm"></rui-form>
            <div slot="footer">
                <Button size="small" @click="isShowModel = false">取消</Button>
                <Button type="primary" size="small" :loading="submitLoading" @click="businessSubmit"
                        v-show="status!='view'">保存
                </Button>
            </div>
            <div v-show="status!='new'">
                <rui-table
                    :defineId="'voucherBusinessDifine'"
                    :fixedLeft="['action']"
                    :showIndex="false"
                    :select="false"
                    :columns="businessChildColumns"
                    :pageAble="false"
                    :slots="[{key:'action',slot:'action'}]"
                    @loadDatas="remoteTableDatas"
                    ref="voucherBusinessChildTable"
                >
                    <template slot="toolBar">
                        <Button @click="addChild" icon="md-add" type="primary" v-show="status!='view'">新增</Button>
                    </template>
                    <template slot="action" slot-scope="{ row }">
                        <div>
                            <Button @click="editChild(row)" size="small" type="primary" v-show="status!='view'">编辑
                            </Button>
                            <Button @click="viewChild(row)" size="small" type="primary">查看</Button>
                            <Button type="error" size="small" @click="removeChild(row)" v-show="status!='view'">删除
                            </Button>
                            <circleLoading v-if="operationChildLoading"/>
                        </div>
                    </template>
                </rui-table>
            </div>
        </Modal>
        <Modal v-model="isShowChildModel" :title="modelTitle" :width="1000" :closable="false"
               :mask-closable="false" @on-ok="businessChildSubmit">
            <rui-form :form-options="businessChildFormOptions" :read-only="false"
                      ref="businessChildForm"></rui-form>
            <div slot="footer">
                <Button size="small" @click="isShowChildModel = false">取消</Button>
                <Button type="primary" size="small" :loading="submitChildLoading" @click="businessChildSubmit"
                        v-show="childStatus!='view'">保存
                </Button>
            </div>
        </Modal>
    </rui-page>
</template>
<script>
    import voucherBusinessDifine from "../defines/voucher-business-define";
    import circleLoading from "_c/circle-loading/circle-loading.vue";
    import {deepClone} from "@/libs/utils/ObjectClone";
    import {
        list,
        delVoucherBusiness,
        addVoucherBusiness,
        modifyVoucherBusiness,
        listBusinessChild
    } from "@/projects/basic/api/contract/voucher/voucherBusiness";

    export default {
        name: "product-business-configuration",
        components: {
            circleLoading
        },
        data() {
            return {
                operationLoading: false,
                submitLoading: false,
                submitChildLoading: false,
                operationChildLoading: false,
                isShowModel: false,
                isShowChildModel: false,
                modelTitle: null,
                status: null,
                childStatus: null,
                path: "contractAccount",
                searchFormOptions: [
                    {
                        defineId: "voucherBusinessDifine",
                        span: 6,
                        fields: ["business", "businessCode"]
                    },
                ],
                businessColumns: [
                    'action', 'businessCode','business',  'businessType', 'businessDescribe', 'createBy', 'createTime', 'updateBy', 'updateTime'
                ],
                businessChildColumns: [
                    'action','businessCode', 'business',  'businessType', 'businessDescribe', 'createBy', 'createTime', 'updateBy', 'updateTime'
                ],
                businessFormOptions: [{
                    grids: [
                        {defineId: "voucherBusinessDifine", span: 12, fields: ["businessCode", "businessType"]},
                        {defineId: "voucherBusinessDifine", span: 12, fields: ["business", "businessDescribe"]},
                    ]
                }],
                businessChildFormOptions: [{
                    grids: [
                        {
                            defineId: "voucherBusinessDifine",
                            span: 12,
                            fields: ["businessCode", "businessType", "mainBusinessCode"]
                        },
                        {
                            defineId: "voucherBusinessDifine",
                            span: 12,
                            fields: ["business", "businessDescribe", "mainBusiness"]
                        },
                    ]
                }],
                searchForm: {pageNumber: 1, pageSize: 10, condition: {business:"1"}}
            };
        },
        computed: {
            defines() {
                return [
                    {
                        id: "voucherBusinessDifine",
                        fields: voucherBusinessDifine
                    }
                ];
            }
        },
        methods: {
            queryData(queryData) {
                console.log(queryData);
                list(queryData, this.path).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.voucherBusinessTable.updateTableData(records, total);
                    }
                });
            },
            refresh() {
                this.$refs.searchForm.setFiledValue("business", null);
                this.$refs.searchForm.setFiledValue("businessCode", null);
                this.$refs['voucherBusinessTable'].resetData();
            },
            add() {
                this.modalType = 0;
                this.modalTitle = "新增";
                this.isShowModel = true;
                this.status = "new";
                this.$refs.businessForm.setAllFieldRequired(true);
                this.$refs.businessForm.setAllFieldReadOnly(false);
                this.$refs.businessForm.setFieldRequired("mainBusinessCode", false);
                this.$refs.businessForm.setFieldReadOnly("businessType", true);
                this.$nextTick(() => {
                    this.$refs.businessForm.resetFrom();
                    this.$refs.businessForm.setFiledValue("businessType", 'main');
                })
            },
            //新增子事件
            addChild() {
                this.modalType = 0;
                this.modalTitle = "新增";
                this.isShowChildModel = true;
                this.childStatus = "new";
                this.$refs.businessChildForm.setAllFieldRequired(true);
                this.$refs.businessChildForm.setAllFieldReadOnly(false);
                this.$nextTick(() => {
                    this.$refs.businessChildForm.resetFrom();
                    this.$refs.businessChildForm.setFiledValue("businessType", 'child');
                    this.$refs.businessChildForm.setFieldReadOnly("businessType", true);
                    this.$refs.businessChildForm.setFiledValue("mainBusinessCode", this.$refs.businessForm.getFormData().businessCode);
                    this.$refs.businessChildForm.setFieldReadOnly("mainBusinessCode", true);
                    this.$refs.businessChildForm.setFiledValue("mainBusiness", this.$refs.businessForm.getFormData().business);
                    this.$refs.businessChildForm.setFieldReadOnly("mainBusiness", true);
                })
            },
            edit(row) {
                this.modalType = 1;
                this.modalTitle = "编辑";
                this.status = "edit";
                this.$refs.businessForm.resetFrom();
                this.$refs.businessForm.updateFormData(deepClone(row));
                this.$refs.businessForm.setAllFieldRequired(true);
                this.$refs.businessForm.setAllFieldReadOnly(false);
                this.$refs.businessForm.setFieldReadOnly("businessType", true);
                this.$refs.businessForm.setFieldRequired("mainBusinessCode", false);
                this.$refs.businessForm.setFieldReadOnly("businessCode", true);
                this.isShowModel = true;
                this.remoteTableDatas();
            },
            editChild(row) {
                this.modalType = 1;
                this.modalTitle = "编辑";
                this.$refs.businessChildForm.resetFrom();
                this.$refs.businessChildForm.updateFormData(deepClone(row));
                this.$refs.businessChildForm.setAllFieldRequired(true);
                this.$refs.businessChildForm.setAllFieldReadOnly(false);
                this.$refs.businessChildForm.setFieldReadOnly("businessType", true);
                this.$refs.businessChildForm.setFieldReadOnly("businessCode", true);
                this.$refs.businessChildForm.setFiledValue("mainBusiness", this.$refs.businessForm.getFormData().business);
                this.$refs.businessChildForm.setFieldReadOnly("mainBusiness", true);
                this.$refs.businessChildForm.setFieldReadOnly("mainBusinessCode", true);
                this.isShowChildModel = true;
                this.childStatus = "edit";
                this.remoteTableDatas();
            },
            view(row) {
                this.$refs.businessForm.resetFrom();
                this.status = "view";
                this.$refs.businessForm.updateFormData(deepClone(row));
                this.$refs.businessForm.setAllFieldRequired(true);
                this.$refs.businessForm.setAllFieldReadOnly(true);
                this.isShowModel = true;
                this.remoteTableDatas();
            },
            viewChild(row) {
                this.$refs.businessChildForm.resetFrom();
                this.childStatus = "view";
                this.$refs.businessChildForm.updateFormData(deepClone(row));
                this.$refs.businessChildForm.setFiledValue("mainBusiness", this.$refs.businessForm.getFormData().business);
                this.$refs.businessChildForm.setAllFieldRequired(true);
                this.$refs.businessChildForm.setAllFieldReadOnly(true);
                this.isShowChildModel = true;
            },
            businessSubmit() {
                this.$refs.businessForm.getForm().validate(valid => {
                    if (valid) {
                        if (this.modalType === 0) {
                            this.submitLoading = true;
                            addVoucherBusiness(this.$refs.businessForm.getFormData(), this.path)
                                .then(res => {
                                    this.submitLoading = false;
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.isShowModel = false;
                                        this.refresh();
                                    }
                                }).catch(() => {
                                this.submitLoading = false;
                            });

                        } else {
                            // 编辑
                            this.submitLoading = true;
                            modifyVoucherBusiness(this.$refs.businessForm.getFormData(), this.path)
                                .then(res => {
                                    this.submitLoading = false;
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.isShowModel = false;
                                        this.refresh();
                                    }
                                }).catch(() => {
                                this.submitLoading = false;
                            });
                        }
                    }
                });
            },
            //子事件保存
            businessChildSubmit() {
                this.$refs.businessChildForm.getForm().validate(valid => {
                    if (valid) {
                        if (this.modalType === 0) {
                            this.submitChildLoading = true;
                            addVoucherBusiness(this.$refs.businessChildForm.getFormData(), this.path)
                                .then(res => {
                                    this.submitChildLoading = false;
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.isShowChildModel = false;
                                        this.remoteTableDatas();
                                    }
                                }).catch(() => {
                                this.submitChildLoading = false;
                            });

                        } else {
                            // 编辑
                            this.submitChildLoading = true;
                            modifyVoucherBusiness(this.$refs.businessChildForm.getFormData(), this.path)
                                .then(res => {
                                    this.submitChildLoading = false;
                                    if (res.code === "0000") {
                                        this.$Message.success("操作成功");
                                        this.isShowChildModel = false;
                                        this.remoteTableDatas();
                                    }
                                }).catch(() => {
                                this.submitChildLoading = false;
                            });
                        }
                    }
                });
            },
            remove(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除业务事件: " + v.businessCode + " ?",
                    onOk: () => {
                        this.operationLoading = true;
                        delVoucherBusiness(v.id, this.path).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.refresh();
                            }
                        }).catch(() => {
                            this.operationLoading = false;
                        });
                    }
                });
            },
            removeChild(v) {
                this.$Modal.confirm({
                    title: "确认删除",
                    content: "您确认要删除业务事件: " + v.businessCode + " ?",
                    onOk: () => {
                        this.operationChildLoading = true;
                        delVoucherBusiness(v.id, this.path).then(res => {
                            this.operationChildLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.remoteTableDatas();
                            }
                        }).catch(() => {
                            this.operationChildLoading = false;
                        });
                    }
                });
            },
            remoteTableDatas() {
                let mainBusinessCode = this.$refs.businessForm.getFormData().businessCode;
                listBusinessChild({mainBusinessCode: mainBusinessCode}, this.path).then(res => {
                    if (res.code === "0000") {
                        this.$refs.voucherBusinessChildTable.updateTableData(res.data);
                    }
                });
            },
        },
        mounted() {
            this.refresh();
        }
    };
</script>
